import { logHttp } from '/@/utils/http/axios';

import {
  AppItem,
  AppParam,
  ComponentItem,
  LabelListItem,
  logFetchResult,
  LogPageParams,
  TenantItem,
  TenantParam,
} from '/@/api/maintenance-center/system-log/model/systemLogsModel';
import moment from 'moment';

enum Api {
  GET_LOG_LIST = '/v1/log/querySystemLog',
  GET_TENANT_LIST = '/v1/log/queryTenantList',
  GET_APP_LIST = '/v1/log/appList',
  GET_COMPONENT_LIST = '/v1/log/componentList',
}

export const getSystemLogList = async (params: LogPageParams) => {
  // 获取系统日志列表
  params.startTimePrimal = moment(params.logTime[0].$d.valueOf()).format('YYYY-MM-DDTHH:mm:ss');
  params.endTimePrimal = moment(params.logTime[1].$d.valueOf()).format('YYYY-MM-DDTHH:mm:ss');
  params.logTime = [];
  const list = await logHttp.post<logFetchResult>({
    url: Api.GET_LOG_LIST,
    params,
  });
  const unitList: LabelListItem[] = [];
  for (let i = 0; i < list.length; i++) {
    const item = {
      hostIP: list[i].labels.hostIP,
      podName: list[i].labels.podName,
      content: list[i].content,
      time: list[i].labels.time,
    };

    const dateTime = new Date(parseInt(list[i].labels.time));
    const year = dateTime.getFullYear();
    const month = dateTime.getMonth() + 1;
    const day = dateTime.getDate();
    const hour = dateTime.getHours();
    let minute = dateTime.getMinutes();
    if (minute < 10) {
      minute = '0' + minute;
    }
    let second = dateTime.getSeconds();
    if (second < 10) {
      second = '0' + second;
    }
    const timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    item.time = timeSpanStr;

    unitList.push(item);
  }
  // 获取列表转化为标准格式
  return unitList;
};

export const getTenantList = () => {
  // 请求tenant
  return logHttp.get<TenantItem[]>({ url: Api.GET_TENANT_LIST });
};

export const getAppListByTenantCode = (params: TenantParam) => {
  // 请求app
  return logHttp.get<AppItem[]>({ url: Api.GET_APP_LIST, params });
};

export const getComponentListByAppId = (params: AppParam) => {
  // 请求组件
  const res = logHttp.get<ComponentItem[]>({
    url: Api.GET_COMPONENT_LIST,
    params,
  });
  return res;
};
